// pages/orderDetails/orderDetails.js
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vip:true,
    addressList:[
      {
        id:1,
        username	:	'li',	
        telphone	:	'16712836619',
        address	:	'河南省郑州市中原区',	
        detailAddress	:	'升龙又一城',	
        IDcardName:'',
        IDNum	:	'',		
        defaultAddress	:	false
      },
      {
        id:2,
        username	:	'zheng',	
        telphone	:	'16712836619',
        address	:	'河南省郑州市中原区',	
        detailAddress	:	'升龙又一城',	
        IDcardName:'',
        IDNum	:	'',		
        defaultAddress	:	true
      }
    ],
    goodsList:[
      {
        cover: "https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/1ae1a03f92d926079a12d28a4a9b4a1f.jpg",
        id: "3366",
        num: 1,
        pintuan: 0,
        price: 259,
        sell_price: 229,
        sku_id: 2887,
        supplier: "自营",
        tags: null,
        title: "YUMC男士背包学生书包电脑旅行双肩背包休闲时尚B2085",
        vip_price: 229,
      },
      {
        cover: "https://yceat-20200506.oss-cn-shanghai.aliyuncs.com/goods/cover/9bddd43bcb40b1464104f4f91b808b22.png",
        id: "3596",
        num: 1,
        price: 79,
        sell_price: 46,
        sku_id: 2965,
        supplier: "自营",
        tags: "初桃粉",
        title: "蕉下   冰薄系列防晒围巾【初桃粉】",
        vip_price: 46
      }
    ],
    i:null,
    address:null,
    couponGroup:{
      ables: [
        {
          id:1,
          amount:"10.00",
          tag:"全场通用券",
          startTime:"2021.03.08",
          endTime:"2021.04.01"
        },
        {
          id:2,
          amount:"15.00",
          tag:"全场通用券",
          startTime:"2021.03.08",
          endTime:"2021.04.01"
        },
        {
          id:3,
          amount:"20.00",
          tag:"全场通用券",
          startTime:"2021.03.08",
          endTime:"2021.04.01"
        }
      ],
      disables: [
        {
          amount:"10.00",
          tag:"全场通用券",
          startTime:"2021.02.08",
          endTime:"2021.02.28"
        }
      ]
    },
    showPopup:false,
    showAddress:false,
    showAddAddress:false,
    radio:1,
    couponAmount:null,//点击优惠券时优惠券的面额
  },

  //判断用户是否设置有默认地址
  getDefaultAddress(){
    const addressList=this.data.addressList;
    addressList.map((item,i)=>{
      if(item.defaultAddress){
        this.setData({
          i,
        })
      }
    })
  },
  onChangeShow(){
    this.setData({
      showPopup:true
    })
  },
  onChangePopup(){
    this.setData({
      showPopup:false
    })
  },

  onSelectAddress(){
    this.setData({
      showAddress:true
    })
  },
  // 地址选择
  onChange(e){
    this.setData({
      radio:e.detail,
    })
  },
  onClickLeft(){
    this.setData({
      showAddress:false
    })
  },
  //点击确定时修改地址
  onClickRight(){
    const id=this.data.radio;
    const addressList=this.data.addressList;
    const i=addressList.findIndex(item=>item.id===id)
    this.setData({
      address:this.data.addressList[i],
      showAddress:false
    })
  },

  onHidePage(){
    this.setData({
      showAddAddress:false
    })
    //重新获取以下地址列表并设置address
  },
  
  onShowAddAddress(){
    this.setData({
      showAddAddress:true
    })
  },

  //获取被点击的优惠券的id
  getAmount(e){
    const { id }=e.detail;
    const ables=this.data.couponGroup.ables;
    const i=ables.findIndex(item=>item.id==id);
    const couponAmount=ables[i].amount;
    this.setData({
      couponAmount,
      showPopup:false
    })
  },
  onClickButton(){

    wx.navigateTo({
      url: '/pages/payment/payment',
    })
  },

  goAddAddress(){
    wx.navigateTo({
      url: '/pages/addAddress/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(this.data.addressList.length){
      this.getDefaultAddress();
      this.setData({
        address:this.data.addressList[this.data.i] || this.data.addressList[0],
      })
    }else{
      Dialog.confirm({
        message: '去添加地址',
      })
        .then(() => {
          this.setData({
            showAddAddress:true
          })
        })
        .catch(() => {
          // on cancel
        });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})